package com.llu.serial.fastjson.deserializer;

import com.llu.serial.fastjson.JsonConfig;
import com.llu.serial.fastjson.Utils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class ListDeserializer implements ObjectDeserializer {

    private final ParameterizedType type;
    public ListDeserializer(ParameterizedType type){
        this.type = type;
    }

    @Override
    public <T> T deserializer(JsonConfig config, String json, Object object) throws Throwable {
        JSONArray jsonArray;
        if(null == object){
            jsonArray = new JSONArray(json);
        }else{
            jsonArray = (JSONArray) object;
        }

        List list = new ArrayList();

        for(int i = 0 ; i < jsonArray.length(); i ++){
            Object itemObj = jsonArray.get(i);
            if(itemObj instanceof JSONArray
                    || itemObj instanceof JSONObject){
                Type itemType = Utils.getItemType(type);
                ObjectDeserializer deserializer = config.getDeserializer(itemType);
                Object item = deserializer.deserializer(config,null,itemObj);
                list.add(item);
            }else{
                list.add(itemObj);
            }
        }
        return (T)list;
    }
}
